<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04员工表格练习</title>
</head>
<body>
    <input type="text" placeholder="请输入员工姓名" id="i1">
    <input type="text" placeholder="请输入员工工资" id="i2">
    <input type="text" placeholder="请输入员工岗位" id="i3">
    <input type="button" value="点我添加员工" onclick="addEmp()">
    <hr>
    <table border="1px">
        <caption>员工表</caption>
        <tr>
            <th>姓名</th>
            <th>工资</th>
            <th>岗位</th>
        </tr>
    </table>
    <script>
        //每次点击添加员工按钮时触发，点击一次，向表格中添加一行输入的员工数据
        function addEmp(){
            //1.拿到用户在输入框中输入的数据
            let uName = document.getElementById('i1').value;
            let uSalary = document.getElementById('i2').value;
            let uJob = document.getElementById('i3').value;
            //8.判断用户输入的信息是否为空，为空则弹出提示
            if(uName==''||uSalary==''||uJob==''){
                alert('请输入完整信息！');
                return;
            }
            //2.创建表格的tr对象
            let tr = document.createElement('tr');
            //3.创建当前行的3个td对象
            let nameTd = document.createElement('td');
            let salaryTd = document.createElement('td');
            let jobTd = document.createElement('td');
            //4.将拿到的数据装入到单元格td中
            nameTd.innerHTML = uName;
            salaryTd.innerHTML = uSalary;
            jobTd.innerHTML = uJob;
            //5.将准备好的td装到tr中
            tr.append(nameTd,salaryTd,jobTd);
            //6.将准备好的tr装到表格table中
            document.querySelector('table').append(tr);
            //7.插入数据后清空输入框
            document.getElementById('i1').value = '';
            document.getElementById('i2').value = '';
            document.getElementById('i3').value = '';
        }
    </script>
</body>
</html>